﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BusinessLayer;
using ModelLayer;
using ShopKart.Areas.User.Models;
using ShopKart.Common;
using ShopKart.Controllers;

namespace ShopKart.Areas.User.Controllers
{

    public class CommonController : BaseController
    {
        // GET: User/Common
        [AuthUser(Roles = "User")]
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public PartialViewResult GetUserCart(long userId)
        {
            UserCartManager userCartManager = new UserCartManager();
            UserCartVm userCartVm = new UserCartVm()
            {
                UserCarts = userCartManager.GetByUser(userId)
            };
            Session["UserCart"] = userCartVm.UserCarts;
            return PartialView("_UserCart", userCartVm);
        }

        [AuthUser(Roles = "User")]
        [HttpPost]
        public JsonResult AddProductToCart(UserCart userCart)
        {
            UserCartManager userCartManager = new UserCartManager();
            userCart.UserId = SessionObject.UserId;
            var result = userCartManager.AddUpdate(userCart);

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [AuthUser(Roles = "User")]
        [HttpPost]
        public JsonResult RemoveProductFromCart(long cartId)
        {
            UserCartManager userCartManager = new UserCartManager();
            var result = userCartManager.Delete(cartId);

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [AuthUser(Roles = "User")]
        public ActionResult CheckOut()
        {
            UserCartManager userCartManager = new UserCartManager();

            CheckOutVm checkOutVm = new CheckOutVm()
            {
                UserCarts = userCartManager.GetByUser(SessionObject.UserId)
            };
            return View("CheckOut", checkOutVm);
        }
    }
}